package com.zjk.controller.product;

import com.github.pagehelper.PageInfo;
import com.zjk.entity.Result;
import com.zjk.pojo.Product;
import com.zjk.pojo.ProductVO;
import com.zjk.service.impl.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    //用户列表
    @RequestMapping("/productList")
    public  String productList(String productName, Model model,
                              @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize",defaultValue = "2")   Integer pageSize
    ){

        List<Product> allProducts = productService.getAllProducts(productName,pageNum,pageSize);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        for (Product product : allProducts){
            String format = sdf.format(product.getDepartureTime());
            product.setDateFormat(format);
        }
        PageInfo<Object> pageInfo = new PageInfo<>(allProducts);

        model.addAttribute("productList",pageInfo.getList());
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("productName",productName);
        return "product_list";
    }

    @RequestMapping("/main")
    public String main(HttpSession httpSession, Model model){
        Product productSession = (Product) httpSession.getAttribute("PRODUCT_SESSION");
        model.addAttribute(productSession);
        return "main";
    }
    //新增用户列表
    @RequestMapping("/addProduct")
    @ResponseBody
    public Result addProduct(ProductVO productVO){
        Result<Object> result = new Result<>();

        if (productVO !=null && productVO.getProductName()!=null
                && !productVO.getProductName().trim().equals("")
                && productVO.getProductNum()!=null
                && !productVO.getProductNum().trim().equals("")){

            Product product1 =productService.getProductByProductName(productVO.getProductName());
            System.out.println(product1);
            if (product1!=null){
                result.setMsg("用户已经存在");
                result.setSuccess(false);
                return result;
            }
            int i = productService.addProduct(productVO.toProduct());
            System.out.println(i);
            if (i>0){
                result.setMsg("新增成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("新增失败");
                result.setSuccess(false);
                return result;
            }
        }else {
            System.out.println("请输入用户名密码");
            result.setMsg("请输入用户名密码");
            result.setSuccess(false);
            return result;
        }
    }

    @RequestMapping("/getProductById")
    @ResponseBody
    public  Result getProductById(Integer id){
        Result<Object> result = new Result<>();
        if (id!=null){
            Product product = productService.getProductById(id);

            if(product!=null){
                result.setData(product);
                result.setMsg("成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("没有此用户");
                result.setSuccess(false);
                return result;
            }
        }

        return result;
    }


    //修改用户列表
    @RequestMapping("/editProduct")
    @ResponseBody
    public Result editProduct(ProductVO productVO){
        Result<Object> result = new Result<>();

        if (productVO !=null && productVO.getProductName()!=null
                && !productVO.getProductName().trim().equals("")
                && productVO.getId()!=null
        ){
            int i = productService.editProduct(productVO.toProduct());
            System.out.println(i);
            if (i>0){
                result.setMsg("修改成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("修改失败");
                result.setSuccess(false);
                return result;
            }
        }
        return result;
    }
    @RequestMapping("/editProductPassword")
    @ResponseBody
    public Result editProductPassword(Product product,HttpSession httpSession){
        Product productSession = (Product) httpSession.getAttribute("PRODUCT_SESSION");
        product.setId(productSession.getId());
        Result<Object> result = new Result<>();

        if (product!=null && product.getProductNum()!=null
                && !product.getProductNum().trim().equals("")
        ){
            int i = productService.editProduct(product);
            System.out.println(i);
            if (i>0){
                result.setMsg("修改成功");
                result.setSuccess(true);
                return result;
            }else {
                result.setMsg("修改失败");
                result.setSuccess(false);
                return result;
            }
        }
        return result;
    }

    //删除用户列表
    @RequestMapping("/delProduct")
    @ResponseBody
    public Result delProduct(Integer id){
        Result<Object> result = new Result<>();
        int i = productService.delById(id);
        if(i>0){
            result.setMsg("删除成功");
            result.setSuccess(true);
            return result;
        }else {
            result.setMsg("新增失败");
            result.setSuccess(false);
            return result;
        }
    }


    //注销
    @RequestMapping("/exitProduct")
    public String exitProduct(HttpSession httpSession){
        httpSession.invalidate();
        return "login";
    }


    //批量删除用户列表
    @RequestMapping("/delAllProduct")
    @ResponseBody
    public Result delAllProduct(String ids){
        Result<Object> result = new Result<>();
        System.out.println("收到删除数据");
        System.out.println(ids);
        String[] strs =ids.split(",");
        List<Integer> id = new ArrayList<>();
        System.out.println(strs.length);
        for(int i = 0;i<strs.length; i++){
            id.add(Integer.parseInt(strs[i].trim()));
//           ids.add(Integer.valueOf(strs[i]));
//            id.add(strs[i].length());
        }

        System.out.println(id.size());
        int i = productService.delAll(id);
        if(i>0){
            result.setMsg("删除成功");
            result.setSuccess(true);
            return result;
        }else {
            result.setMsg("删除失败");
            result.setSuccess(false);
            return result;
        }

    }
}
